#include "view_time_14.h"
#include "view_time_14.inc"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_times.h"
#include "math.h"
#include "weather_ctrl.h"
#ifdef TIME_14_SKIP
#else
static void show_times(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.com_data.i_temp[31] = (RTCGetSeconds() % 60) * 6;
        uint16_t temp16 = get_present_day_minute();
        disp_ctrl.com_data.i_temp[32] = (temp16 / 2) / 6 * 6 % 360; //时针的角度
        disp_ctrl.com_data.i_temp[33] = (temp16 * 6) / 6 * 6 % 360; //分针的角度
    }
    disp_draw_img_rotate(gImage_view_time14_hour, 0, 57, (119 << 16) | 119, disp_ctrl.com_data.i_temp[32]);   //-3
    disp_draw_img_rotate(gImage_view_time14_minute, 0, 74, (119 << 16) | 119, disp_ctrl.com_data.i_temp[33]); //-7
    disp_draw_img_rotate(gImage_view_time14_second, 0, 94, (119 << 16) | 119, disp_ctrl.com_data.i_temp[31]);
}

void show_percent(uint8_t const *full, int percent, int x, int y)
{
    if (percent < 0)
    {
        percent = 0;
    }
    else if (percent > 100)
    {
        percent = 100;
    }
    //#warning debug
    //    percent = 25;
    disp_draw_user_image_percent_circle(x, y, full, percent, 28);
}
void view_time_14_display(void)
{
    int month = 0;
    int day = 0;
    int steps = 0;
    int calories = 0;
    int heart_rate = 0;
#if 0
#warning debug
    steps = 13123;
    calories = 312;
    heart_rate = 132;
#else
    month = disp_ctrl.com_data.i_temp[1];
    day = disp_ctrl.com_data.i_temp[2];
    steps = disp_ctrl.com_data.i_temp[11];
    calories = disp_ctrl.com_data.i_temp[16];
    heart_rate = disp_ctrl.com_data.i_temp[17];
#endif
    int x = 0;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_hm_yq_2);

    show_time_0_battery(disp_ctrl.com_data.i_temp[7], disp_ctrl.com_data.i_temp[8]);
    show_time_0_ble();
    disp_draw_user_image_filter_color(100, 35, img_time_0_week_TAB[disp_ctrl.com_data.i_temp[5] - 1]);
    x = 92;
    y = 60;
    disp_draw_user_image_num_with_digits_filter_color(x, y, month, 2, 12, num_11x18_TAB);
    x = 126;
    y = 60;
    disp_draw_user_image_num_with_digits_filter_color(x, y, day, 2, 12, num_11x18_TAB);
    x = 64;
    y = 120; // steps calorie
    disp_draw_user_image_num_mid_filter_color(x, y, steps, 9, num_8x13_TAB);
    x = 180;
    y = 120; // steps calorie
    disp_draw_user_image_num_mid_filter_color(x, y, calories, 9, num_8x13_TAB);
    y = 180;
    if (heart_rate < 40 || heart_rate > 220)
    {
        int const font_height = 13;
        int const font_width = 8;
        x = 120 - 9;
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + 1;
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
    }
    else
    {
        x = 120;
        disp_draw_user_image_num_mid_filter_color(x, y, heart_rate, 9, num_8x13_TAB);
    }
    x = 31;
    y = 87;
    show_percent(steps_circle, steps * 100 / system_config_get_target_step(), x, y);
    x = 146;
    y = 87;
    show_percent(calorie_circle, calories * 100 / system_config_get_target_calorie(), x, y);
    x = 87;
    y = 142;
    int max_heart_rate = 220 - system_config_get_age();
    int min_heart_rate = 40;
    show_percent(heart_rate_circle, (heart_rate - min_heart_rate) * 100 / (max_heart_rate - min_heart_rate), x, y);

    show_times();

    disp_draw_user_image_filter_color(113, 113, circle_113_113);
    disp_draw_user_image(116, 116, Center_116_116);
}
#endif
